package org.jeecg.modules.api.withdraw.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.common.system.vo.MemberModel;
import org.jeecg.config.app.AppContext;
import org.jeecg.modules.api.member.entity.Member;
import org.jeecg.modules.api.member.mapper.MemberMapper;
import org.jeecg.modules.api.member.service.IMemberService;
import org.jeecg.modules.api.order.entity.Order;
import org.jeecg.modules.api.withdraw.entity.WithdrawRecord;
import org.jeecg.modules.api.withdraw.mapper.WithdrawRecordMapper;
import org.jeecg.modules.api.withdraw.service.IWithdrawRecordService;
import org.jeecg.modules.helper.MemberHelp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * @Description: 提现记录
 * @Author: jeecg-boot
 * @Date:   2024-10-21
 * @Version: V1.0
 */
@Service
public class WithdrawRecordServiceImpl extends ServiceImpl<WithdrawRecordMapper, WithdrawRecord> implements IWithdrawRecordService {
    @Autowired
    private MemberHelp memberHelp;
    @Autowired
    private MemberMapper memberMapper;
    @Autowired
    private  WithdrawRecordMapper  withdrawRecordMapper;
    @Override
    @Transactional
    public WithdrawRecord submit(String req) {
        JSONObject jsonObject = JSONObject.parseObject(req);
        BigDecimal amount = jsonObject.getBigDecimal("amount");
        Member member = memberHelp.getMember();
        int res = memberMapper.subWithdrawMoney(member.getId(),amount);
        if(res==0){
            throw new JeecgBootException("提现余额不足!");
        }
        WithdrawRecord record = new WithdrawRecord();
        record.setCreateTime(new Date());
        record.setUpdateTime(new Date());
        record.setMemberId(member.getId());
        record.setMoney(amount);
        record.setState(0);//待审核
        record.setSysOrgCode(member.getSysOrgCode());
        record.setOpenid(member.getOpenid());
        this.save(record);
        //领取红包
        return record;
    }

    @Override
    public Page<WithdrawRecord> getRecordList(int pageNo, int pageSize) {
        MemberModel memberModel = AppContext.get();
        LambdaQueryWrapper<WithdrawRecord> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(WithdrawRecord::getMemberId, String.valueOf(memberModel.getId()));
        queryWrapper.orderByDesc(WithdrawRecord::getCreateTime);
        Page<WithdrawRecord> page = new Page<WithdrawRecord>(pageNo, pageSize);
        return this.page(page);
    }

    @Override
    public int updatState(String id, int prevState, int state) {
        return withdrawRecordMapper.updatState(id,prevState,state);
    }
}
